package com.demo2do.fms.admin.dao.business;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.GenericJdbcSupport;
import com.demo2do.fms.admin.dto.OtherFeeInfo;
import com.demo2do.fms.entity.business.balance.OtherFee;
import com.demo2do.fms.entity.data.business.OtherFeeType;

/**
 *
 * @author Ark
 *
 */
@Repository("otherFeeDaoSupport")
public class OtherFeeDaoSupport {
    
    @Autowired
    private GenericJdbcSupport genericJdbcSupport;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /**
     * 获取其他费用列表
     * 
     * @param anjiSet
     * @param dispatchOrderId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<OtherFee> list(boolean anjiSet, Long dispatchOrderId) {
        
        String sentence = "FROM OtherFee WHERE anjiSet = :anjiSet and dispatchOrderId = :dispatchOrderId";
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("anjiSet", anjiSet);
        parameters.put("dispatchOrderId", dispatchOrderId);
        
        return genericDaoSupport.searchForList(sentence, parameters);
        
    }
    
    /**
     * list other_fee_type_ids
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Long> listOtherFeeTypeIds() {
        String sentence = "SELECT id FROM OtherFeeType WHERE disabled = false ORDER BY id";
        return genericDaoSupport.searchForList(sentence);
    }
    
    /**
     * list other_fee_types
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<OtherFeeType> list() {
    	String sentence = "FROM OtherFeeType WHERE disabled = false ORDER BY id";
        return genericDaoSupport.searchForList(sentence);
    }
    
    /**
     * load other_fee by other_fee_type_id, dispatch_order_id and anji_set
     * 
     * @param otherFeeTypeId
     * @param dispatchOrderId
     * @param anjiSet
     * @return
     */
    public OtherFee load(Long otherFeeTypeId, Long dispatchOrderId, Boolean anjiSet) {
        
        String sentence = "FROM OtherFee f WHERE f.otherFeeTypeId = :otherFeeTypeId AND f.dispatchOrderId = :dispatchOrderId AND f.anjiSet = :anjiSet";
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("otherFeeTypeId", otherFeeTypeId);
        parameters.put("dispatchOrderId", dispatchOrderId);
        parameters.put("anjiSet", anjiSet);
        
        return genericDaoSupport.searchForObject(sentence, parameters, OtherFee.class);
        
        
    }
    
    /**
     * 获取其他费用信息列表
     * 
     * @param dispatchOrderId
     * @return
     */
    public List<OtherFeeInfo> getOtherFeeMappings(Long dispatchOrderId) {
        
        String sentence = "SELECT A.id, A.fee_name, F.other_fee, F.IS_ANJI_SET AS anji_set, A.dispatch_order_id FROM (SELECT T.ID AS id, T.NAME AS fee_name, D.ID AS DISPATCH_ORDER_ID FROM TBL_TRUCK_OTHER_FEE_TYPE T, TBL_TRUCK_DISPATCH_ORDER D WHERE T.IS_DISABLED = 0) A LEFT JOIN TBL_TRUCK_DISPATCH_OTHER_FEE F ON (A.id = F.OTHER_FEE_TYPE_ID AND A.DISPATCH_ORDER_ID = F.DISPATCH_ORDER_ID) WHERE A.DISPATCH_ORDER_ID = :dispatchOrderId ORDER BY A.id";
        
        List<OtherFeeInfo> otherFeeInfos = genericJdbcSupport.queryForList(sentence, "dispatchOrderId", dispatchOrderId, OtherFeeInfo.class);
        return otherFeeInfos;
        
    }
    
    /**
     * 获取其他费用(FC_GET_OTHER_FEE)
     * 
     * @param dispatchOrderId
     * @return
     */
    public Object getOtherFee(Long dispatchOrderId) {
        
        String sentence = "SELECT FC_GET_OTHER_FEE(:dispatchOrderId) AS fee FROM DUAL";
        
        Map<String, Object> result = genericJdbcSupport.queryForObject(sentence, "dispatchOrderId", dispatchOrderId);
        return result.get("fee");
        
    }
    
    /**
     * 获取指定类型的其他费用(FC_GET_OTHER_FEE_DETAIL)
     * 
     * @param dispatchOrderId
     * @param otherFeeTypeId
     * @return
     */
    public Object getOtherFeeDetail(Long dispatchOrderId, Long otherFeeTypeId) {
    	
    	String sentence = "SELECT FC_GET_OTHER_FEE_DETAIL(:dispatchOrderId, :otherFeeTypeId) AS fee FROM DUAL";
    	
    	Map<String, Object> parameters = new HashMap<String, Object>();
    	parameters.put("dispatchOrderId", dispatchOrderId);
    	parameters.put("otherFeeTypeId", otherFeeTypeId);
    	
    	Map<String, Object> result = genericJdbcSupport.queryForObject(sentence, parameters);
    	return result.get("fee");
    	
    }
    
}
